<template>
  <div class="web-main">
    <!--功能-->
    <el-row class="web-header">
      <el-col :span="16">
        <!--<div class="upload-excel-box" style="float: left;margin-right: 13px;">-->
          <!--<form action="" method="post" enctype="multipart/form-data" >-->
            <!--<input type="file"  id="excelUploadInput" v-if="isUploadExcel" accept=".xlsx, .xls"-->
                   <!--ref="excelUploadInput"-->
                   <!--class="c-hide upload-excel-input"-->
                   <!--@change="cBillImportExcel">-->
          <!--</form>-->
          <!--<el-button type="primary">导入车辆</el-button>-->
        <!--</div>-->
        <el-button type="primary" @click="editBtn('新增',selectOneRow,'detail')">新增</el-button>
        <el-button type="primary" @click="statusBtn">禁用</el-button>
        <el-button type="primary" @click="enableBtn">启用</el-button>
        <!--<el-button type="primary" @click="downloadTemplateExcel">下载模板</el-button>-->
      </el-col>
      <!--<el-col :span="8" style="text-align: right">-->
        <!--<el-input style="width: 180px" v-model="page.condition.fleetId" @keyup.enter.native="search" placeholder="编号"></el-input>-->
        <!--<el-button type="primary" @click="moreSearch">-->
          <!--<span v-if="!searchFlag">更多</span>-->
          <!--<span v-else>收起</span>-->
        <!--</el-button>-->
      <!--</el-col>-->
    </el-row>
    <!--查询条件-->
    <el-form class="web_form" :inline="true" :class="{'web_form_active': searchFlag }" label-width="80px" style="margin: 6px 0 0 10px">
      <el-form-item label="车牌号">
        <el-input v-model="page.condition.plateLicenseNo"  clearable placeholder="请输入" style="width: 184px"></el-input>
      </el-form-item>
      <el-form-item label="车架号">
        <el-input type="textarea" :rows="1" style="width: 184px"
                  v-model="page.condition.vin"  placeholder="请输入, 以英文逗号来分隔"></el-input>
      </el-form-item>
      <!--<el-form-item label="动力类型">-->
        <!--<el-select v-model="page.condition.powerType" class="w184" clearable filterable remote reserve-keyword placeholder="请选择状态">-->
          <!--<el-option key="1" label="电动" value="BLADE_ELECTRIC_VEHICLE"></el-option>-->
          <!--<el-option key="2" label="混合动力" value="HYBRID_ELECTRICAL_VEHICLE"></el-option>-->
          <!--<el-option key="3" label="燃油" value="GAS_VEHICLE"></el-option>-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="制造商">
        <el-input v-model="page.condition.makerName"  clearable placeholder="请输入" style="width: 184px"></el-input>
      </el-form-item>
      <el-form-item label="品牌">
        <el-input v-model="page.condition.brandName"  clearable placeholder="请输入" style="width: 184px"></el-input>
      </el-form-item>
      <el-form-item label="车系">
        <el-input v-model="page.condition.seriesName"  clearable placeholder="请输入" style="width: 184px"></el-input>
      </el-form-item>
      <el-form-item label="车型">
        <el-input v-model="page.condition.modelName"  clearable placeholder="请输入" style="width: 184px"></el-input>
      </el-form-item>
      <el-form-item label="车队">
        <el-input v-model="page.condition.fleet"  clearable placeholder="请输入" style="width: 184px"></el-input>
      </el-form-item>
      <el-form-item label="启用状态">
        <el-select v-model="page.condition.status" class="w184" clearable filterable remote reserve-keyword placeholder="请选择状态">
          <el-option key="1" label="正常" value="10"></el-option>
          <el-option key="2" label="禁用" value="40"></el-option>
          <!--<el-option key="3" label="删除" value="50"></el-option>-->
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker class="w184"
                        v-model="page.condition.startDate"
                        type="datetime"
                        value-format = 'yyyy-MM-dd HH:mm:ss'
                        placeholder="选择日期">
        </el-date-picker>-
        <el-date-picker class="w184"
                        v-model="page.condition.endDate"
                        type="datetime"
                        value-format = 'yyyy-MM-dd HH:mm:ss'
                        placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item class="search-box">
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>
    <!--主列表-->
    <div style="width: 100%;position: relative;">
      <el-table
        :data="tableData"
        ref="mainTable"
        header-row-class-name="table-header-center"
        border element-loading-text="正在加载中..."
        @selection-change="handleSelectionChange"
        style="margin: 0 4px;">
        <el-table-column type="selection" width="40"></el-table-column>
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column :key="key" align="center" :width="value.width" v-for='(value,key) in tableHead'
                         :label="value.title">
          <template slot-scope="scope">
            <!--<div v-if="value.filter">{{scope.row[value.key] | geval(value.filter)}}</div>-->
            <!--<a v-if="value.key === 'no'" href="#" @click="getInfoList(scope.row)" style="color: #409EFF; font-size: 12px;">{{scope.row.no}}</a>-->
            <div v-if="value.key === 'createTime'">{{scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}')}}</div>
            <div v-else-if="value.key === 'vehicleStatus'">{{scope.row.vehicleStatus | statusFilter}}</div>
            <div v-else>{{scope.row[value.key]}}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="editBtn('查看',scope.row,'')">查看</el-button>
            <el-button type="text" size="mini" @click="editBtn('编辑',scope.row,'detail')">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <el-pagination
      style="margin-top: 8px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total">
    </el-pagination>
    <!-- 新增/编辑/查看 车型详情 面板-->
    <car-detail-edit
      :edit_flag="editFlag"
      :edit_params="editParams"
      :edit_main_title="editMainTitle"
      :edit_select_row="editRowData"
      ref="carDetailRef"
      @edit-confirm="editConfirm"
      @edit-close="editClose">
    </car-Detail-edit>
    <!-- 占位 -->
    <div style="height: 50px"></div>
  </div>
</template>

<script>
  import CarDetailEdit from './carDetailConfig/carDetailEdit'
  import * as CarDeployMgtAPI from '@/api/carDeploy'
  import ReadExcel from '@/utils/readExcel'
  import baseURL from '@/utils/baseURLConfig'
  import Axios from 'axios'
  // import * as Utils from '@/utils/index'
  const defaultTableHead = [
    // { key: 'no', title: '车辆编号', width: '150' },
    { key: 'vin', title: '车架号', width: '160' },
    { key: 'fleet', title: '车队', width: '160' },
    { key: 'plateLicenseNo', title: '车牌号', width: '' },
    // { key: 'makerNo', title: '制造商编号', width: '' },
    { key: 'makerName', title: '制造商名称', width: '' },
    // { key: 'brandNo', title: '品牌编号', width: '' },
    { key: 'brandName', title: '品牌名称', width: '' },
    // { key: 'seriesNo', title: '车系编号', width: '' },
    { key: 'seriesName', title: '车系名称', width: '' },
    // { key: 'modelNo', title: '车型编号', width: '' },
    { key: 'modelName', title: '车型名称', width: '' },
    { key: 'admissionType', title: '发动机进气方式', width: '' },
    { key: 'fuelLabel', title: '燃油标号', width: '' },
    // { key: 'fuelTankage', title: '邮箱容量L', width: '' },
    { key: 'displacement', title: '排量', width: '' },
    // { key: 'horsepower', title: '马力', width: '' },
    { key: 'mixedFrom', title: '混合形式', width: '' },
    // { key: 'year', title: '年代', width: '' },
    { key: 'wheelbase', title: '轴距', width: '' },
    { key: 'transmissionType', title: '变速箱类型', width: '' },
    { key: 'vehicleStatus', title: '状态', width: '', filter: 'statusFilter' },
    { key: 'description', title: '备注', width: '' },
    { key: 'createTime', title: '创建时间', width: '150', filter: 'parseTime("{y}-{m}-{d} {h}:{i}:{s}")' }
  ]
  export default {
    name: '',
    components: { CarDetailEdit },
    data() {
      return {
        tableHead: defaultTableHead,
        searchFlag: true,
        tableData: [],
        selectRow: [],
        selectOneRow: {},
        page: { current: 1, size: 10, total: 1,
          condition: {
            fleet: '',
            makerName: '',
            brandName: '',
            seriesName: '',
            modelName: '',
            plateLicenseNo: '',
            vin: '',
            powerType: '',
            status: '',
            startDate: '',
            endDate: ''
          }
        },
        isUploadExcel: true, // 控制销毁 上传
        editFlag: false, // 明细显示
        editParams: {},
        editRowData: {}, // 传入明细的当前列
        editMainTitle: '' // 明细标题
      }
    },
    created() {
      this.getList()
    },
    methods: {
      // 下载模板
      downloadTemplateExcel() {
        const url = '../../../static/data/excel/车辆模块.xlsx'
        const name = '车辆模块'
        const nowA = document.createElement('a')
        nowA.setAttribute('href', url)
        nowA.setAttribute('download', name)
        nowA.style.display = 'none'
        document.body.appendChild(nowA)
        nowA.click()
        document.body.removeChild(nowA)
      },
      // 导入考核单 方式1
      cBillImportExcel(e) {
        const that = this
        const file = e.target.files[0]
        console.log('file', file)
        // const fileName = file.name
        if (that.$refs.excelUploadInput.files.length !== 0) {
          that.isUploadExcel = false
          const NewFromData = new FormData()
          NewFromData.append('file', file)
          Axios({
            url: baseURL.AUTO + 'vehicles/importVehicleExcel',
            method: 'post',
            headers: {
              'Content-Type': 'multipart/form-data'
            },
            data: NewFromData
          }).then(res => {
            that.$message({
              message: '导入成功',
              type: 'success',
              duration: 2000
            })
            that.isUploadExcel = true
            that.getList()
          }).catch(err => {
            that.$message({
              message: '导入失败' + err,
              type: 'error',
              duration: 2000
            })
            that.isUploadExcel = true
          })
        }
      },
      // 导入考核单 方式2
      cBillImportExcel2(e) {
        const that = this
        that.isUploadExcel = false
        ReadExcel.readerData(e, function(excelData) {
          const toParams = {
            header: excelData.header,
            rows: excelData.results
          }
          that.isUploadExcel = true
          CarDeployMgtAPI.importVehicleExcel(toParams).then(result => {
            that.$message({
              message: '导入成功',
              type: 'success',
              duration: 2000
            })
            that.getList()
          }).catch(() => {})
        })
      },
      // 新增  编辑  查看 ,  make  brand
      editBtn(editTitle, row, contentType) {
        const that = this
        that.editRowData = row
        that.editMainTitle = editTitle
        that.editParams = {
          type: contentType
        }
        that.editFlag = true
      },
      // 编辑面板 确认
      editConfirm() {
        this.getList()
        this.editFlag = false
      },
      // 编辑面板 弹窗关闭
      editClose() {
        this.getList()
        this.editFlag = false
      },
      // 禁用
      statusBtn() {
        if (this.checkSelect(this.selectRow)) {
          const ids = []
          this.selectRow.map(item => {
            ids.push(item.vehivleId.toString())
          })
          CarDeployMgtAPI.vehiclesForbiddenByIds({ ids: ids.length > 1 ? ids.join(',') : ids[0] }).then(res => {
            this.$message({
              message: '禁用成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(err => {
            console.log('error', err)
          })
        }
      },
      // 启用
      enableBtn() {
        if (this.checkSelect(this.selectRow)) {
          const ids = []
          this.selectRow.map(item => {
            ids.push(item.vehivleId.toString())
          })
          CarDeployMgtAPI.vehiclesEnableByIds({ ids: ids.length > 1 ? ids.join(',') : ids[0] }).then(res => {
            this.$message({
              message: '启用成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(err => {
            console.log('error', err)
          })
        }
      },
      // 删除
      deleteBtn() {
        if (this.checkSelect(this.selectRow)) {
          const ids = []
          this.selectRow.map(item => {
            ids.push(item.id.toString())
          })
          CarDeployMgtAPI.vehiclesDeleteByIds({ ids: ids.length > 1 ? ids.join(',') : ids[0] }).then(res => {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(err => {
            console.log('error', err)
          })
        }
      },
      // 列表查询
      getList() {
        const that = this
        CarDeployMgtAPI.getVehiclesListNew(that.page).then(res => {
          const t = res.data
          that.tableData = t.records
          that.page.current = t.current
          that.page.size = t.size
          that.page.total = t.total
        }).catch(err => {
          console.log('error', err)
        })
      },
      // 更多
      moreSearch() {
        this.searchFlag = !this.searchFlag
      },
      // 查询
      search() {
        this.page.current = 1
        this.page.size = 10
        this.getList()
      },
      handleSizeChange(val) {
        this.page.size = val
        this.page.current = 1
        this.getList()
      },
      handleCurrentChange(val) {
        this.page.current = val
        this.getList()
      },
      // 重置
      resetSearch() {
        this.page.current = 1
        this.page.size = 10
        this.page.condition = {
          fleet: '',
          makerName: '',
          brandName: '',
          seriesName: '',
          modelName: '',
          plateLicenseNo: '',
          vin: '',
          powerType: '',
          status: '',
          startDate: '',
          endDate: ''
        }
        this.getList()
      },
      // 检查是否选择单条
      checkSelect(data) {
        const l = data.length
        if (l === 0) {
          this.$message({
            message: '未选择',
            type: 'warning',
            duration: 2000
          })
          return false
        }
        return true
      },
      // 表格改变时
      handleSelectionChange(val) {
        this.selectRow = val
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /*upload*/
  .upload-excel-box{
    position: relative;
    overflow: hidden;
  }
  .upload-excel-input{
    opacity: 0;
    width: 100%;
    height: 100%;
    background-size: 100%;
    position: absolute;
    top: 0;
    left: 0;
  }
  .el-table::before {
    left: 0;
    bottom: 0;
    width: 100%;
    height: auto;
  }
</style>
